package lapsnake.model.stmt;

import lapsnake.model.data.*;

import java.util.ArrayList;
import java.util.List;

import lapsnake.model.*;
import lapsnake.model.data.ChildField;
import lapsnake.model.data.SingleChild;
import lapsnake.model.expr.LapName;
import lapsnake.py.ast.ASTHelperFactory; import java.beans.PropertyVetoException;

import org.python.antlr.ast.*;
import org.python.antlr.base.expr;
import org.python.antlr.base.stmt;

/**Wrapper class with helper functions for ClassDef nodes*/
public class LapClassDef extends LapStatement {
	public final SingleChild<LapName> name = new SingleChild<>(this, LapName.class);
	public final ChildList<LapExpression> bases = new ChildList<>(this, LapExpression.class, true);
	public final ChildList<LapStatement> body = new ChildList<>(this, LapStatement.class, false);
	public final ChildList<LapExpression> decorators = new ChildList<>(this, LapExpression.class, true, false);

	public LapClassDef(PyAST pyAST, ChildField<? super LapClassDef> parentField, ClassDef node) throws PropertyVetoException {
		super(pyAST, parentField, node);
		ASTHelperFactory.makeHelper(pyAST, name, node.getInternalNameNode());
		List<expr> baseList = node.getInternalBases();
		for(expr b : baseList)
			ASTHelperFactory.makeHelper(pyAST, bases, b);
		List<stmt> bodyList = node.getInternalBody();
		for(stmt s : bodyList)
			ASTHelperFactory.makeHelper(pyAST, body, s);
		List<expr> decoratorList = node.getInternalDecorator_list();
		for(expr d : decoratorList)
			ASTHelperFactory.makeHelper(pyAST, decorators, d);

		buildTextNodes(node);
	}
	public LapClassDef(PyAST pyAST, ChildField<? super LapClassDef> parentField, String name) throws PropertyVetoException
	{
		super(pyAST, parentField, null);
		new LapName(pyAST, this.name, name);
		new LapPass(pyAST, this.body);
	}
	
	public String getName() {
		return name.toString();
	}
	
	public LapFunctionDef[] getMethods() {
		
		ArrayList<LapFunctionDef> methodList = new ArrayList<>();
		
		for(LapStatement s : body)
		{
			if(s instanceof LapFunctionDef)
				methodList.add((LapFunctionDef) s);
		}
		return methodList.toArray(new LapFunctionDef[methodList.size()]);
	}
}
